package com.company.cloud.user.modules.app.controller;

import java.util.List;
import java.util.Set;

import com.company.cloud.user.modules.api.response.PermissionModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.redis.service.RedisUtils;
import com.company.cloud.common.tree.CloudTree;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.user.constant.PermissionTypeEnum;
import com.company.cloud.user.constant.UserConstant;
import com.company.cloud.user.modules.app.controller.req.MenuGetParam;
import com.company.cloud.user.modules.app.utils.UserUtils;
import com.company.cloud.user.modules.base.entity.PermissionInfo;
import com.company.cloud.user.modules.base.service.PermissionInfoService;
import com.company.cloud.user.modules.cache.service.UserPermissionCacheService;
import com.company.cloud.user.modules.cache.utils.UserConvertUtils;
import com.company.cloud.user.modules.mgr.mapper.UserRolePermissionMapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

@Log4j2
@Login
@RestController
@RequestMapping("/app/user/checkPre")
@Api(tags="User_App_用户权限校验")
public class UserPermissionCheckController {
	
	@Autowired
	private PermissionInfoService permissionInfoService;
	@Autowired
	private UserPermissionCacheService userPermissionCacheService;
	@Autowired
	private UserRolePermissionMapper userRolePermissionMapper;
	@Autowired
	private RedisUtils redisUtils;
	
	@GetMapping("/getMenu2Button")
	@ApiOperation(value="获取菜单和按钮", response=PermissionModel.class)
	public BaseResult getMenu2Button(MenuGetParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		List<PermissionInfo> permissionList=userRolePermissionMapper.queryUserMenu(new QueryWrapper<PermissionInfo>()
				.eq("a.user_id", userInfo.getId())
				.in("c.type", PermissionTypeEnum.P_Menu.getCode(),PermissionTypeEnum.P_Button.getCode())
				.orderByDesc("c.parent_code","c.orders"));
		List<PermissionModel> data= this.getTree(permissionList);
		return BaseResult.ok(data);
	}
	
	
	@GetMapping("/getMenu")
	@ApiOperation(value="获取菜单", response=PermissionModel.class)
	public BaseResult getMenu(MenuGetParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		List<PermissionInfo> permissionList=userRolePermissionMapper.queryUserMenu(new QueryWrapper<PermissionInfo>()
				.eq("a.user_id", userInfo.getId())
				.in("c.type", PermissionTypeEnum.P_Menu.getCode())
				.orderByDesc("c.parent_code","c.orders"));
		List<PermissionModel> data= this.getTree(permissionList);
		return BaseResult.ok(data);
	}
	
	
	
//	@GetMapping("/check")
//	@ApiOperation(value="用户权限校验")
//	public BaseResult get(@RequestParam("code")String code) {
//		LoginUserInfo  userInfo= AuthContextUtils.getUser();
//		return BaseResult.ok();
//	}
	
	@GetMapping("/getUserPermCodes")
	@ApiOperation(value="获取用户权限码")
	public BaseResult getUserPermCodes() {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		LoginUserInfo loginUserInfo=UserUtils.getLoginUserByCache(userInfo.getToken(), redisUtils);
		Set<String> data= userPermissionCacheService.getPermCodeByUser(loginUserInfo);
		return BaseResult.ok(data);
	}

	
	private List<PermissionModel> getTree(List<PermissionInfo> permissionList) {
		CloudTree<PermissionInfo, PermissionModel> tree=new CloudTree<PermissionInfo, PermissionModel>() {
			@Override
			public boolean isTop(PermissionModel entity) {
				return UserConstant.PERMISSION_TOP_CODE.equals(entity.getParentCode());
			}
			@Override
			public boolean isParent(PermissionModel parentObj, PermissionModel childObj) {
				return parentObj.getCode().equals(childObj.getParentCode());
			}
			@Override
			public PermissionModel convert(PermissionInfo entity) {
				return UserConvertUtils.getPermissionModel(entity);
			}
		};
		return tree.tree(permissionList);
	}
	

}
